//
//  taocanViewController.m
//  UI_HLJ_Frame
//
//  Created by  毕宏伟 on 15/7/6.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "taocanViewController.h"
#import "AFNetworking.h"
#import "zhekouMediaModel.h"
#import "zhekouDetailCollectionViewCell.h"
#import "taocanworkModel.h"
#import "UIImageView+WebCache.h"
#import "zhikouonePageTableViewCell.h"
#import "zhekoumerModel.h"
#import "zhikoutwoPageTableViewCell.h"
#import "zhekouworkitemModel.h"
#import "placesModel.h"
@interface taocanViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)UICollectionView *collectionView;
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *zhekouArr;
@property(nonatomic,retain)NSMutableArray *taocanArr;
@property(nonatomic,retain)NSMutableArray *mediaPicArr;
@property(nonatomic,retain)NSMutableArray *zhekoumerArr;
@property(nonatomic,retain)NSMutableArray *workitemArr;
@property(nonatomic,retain)UIView *headerView;
@property(nonatomic,retain)NSMutableArray *placesArr;
@end

@implementation taocanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    
    [self createData];
    [self createView];
}

- (void)createData
{
    self.zhekouArr = [NSMutableArray array];
    self.taocanArr = [NSMutableArray array];
    self.mediaPicArr =[NSMutableArray array];
    self.zhekoumerArr = [NSMutableArray array];
    self.workitemArr = [NSMutableArray array];
    self.placesArr =[NSMutableArray array];
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager GET:[NSString stringWithFormat:@"http://hunliji.com/v1/api/app/works/%@",self.bid] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic =responseObject;
        NSDictionary *workdic = dic[@"work"];
        NSArray *mediaArr =workdic[@"media_items"];
        
        taocanworkModel *taocan =[[taocanworkModel alloc]init];
        [taocan setValuesForKeysWithDictionary:workdic];
        [self.taocanArr addObject:taocan];
        [taocan release];
        
        for (NSDictionary *mediadic in mediaArr) {
            zhekouMediaModel *zhe =[[zhekouMediaModel alloc]init];
            [zhe setValuesForKeysWithDictionary:mediadic];
            [self.zhekouArr addObject:zhe];
            [self.mediaPicArr addObject:zhe.media_path];
            [zhe release];
            
            
        }
        NSDictionary *merchantdic = workdic[@"merchant"];
        
        zhekoumerModel *zkmer = [[zhekoumerModel alloc]init];
        [zkmer setValuesForKeysWithDictionary:merchantdic];
        [self.zhekoumerArr addObject:zkmer];
        [zkmer release];
        
        NSArray *workitemArr =workdic[@"work_items"];
        for (NSDictionary *workitemdic in workitemArr) {
            zhekouworkitemModel *zkitem =[[zhekouworkitemModel alloc]init];
            [zkitem setValuesForKeysWithDictionary:workitemdic];
            [self.workitemArr addObject:zkitem];
            
            [zkitem release];
        }
        
        NSArray *placesarr =workdic[@"places"];
        for (NSDictionary *placesdic in placesarr) {
            placesModel *pl =[[placesModel alloc]init];
            [pl setValuesForKeysWithDictionary:placesdic];
            
            [self.placesArr addObject:pl];
            [pl release];
        }

        [self.collectionView reloadData];
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
}

- (void)createView
{
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.minimumInteritemSpacing =0;
    flow.minimumLineSpacing =0;
    flow.itemSize =CGSizeMake(self.view.frame.size.width, 350);
    [flow setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    self.collectionView =[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 350) collectionViewLayout:flow];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
//    self.collectionView.collectionViewLayout =flow;
    self.collectionView.delegate = self;
    
    self.collectionView.dataSource =self;
    
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    
    self.collectionView.pagingEnabled =YES;
    [self.collectionView registerClass:[zhekouDetailCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
   
    //创建一个tableview
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    
    self.tableView.delegate =self;
    self.tableView.dataSource =self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    
    self.tableView.tableHeaderView =self.collectionView;
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.mediaPicArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    zhekouDetailCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    //zhekouMediaModel *zk = self.zhekouArr[indexPath.row];
    [cell.BigImage sd_setImageWithURL:[NSURL URLWithString:self.mediaPicArr[indexPath.row]]];
    taocanworkModel *tc = self.taocanArr[0];
    cell.titleLabel.text =tc.title;
    cell.kindLabel.text =tc.kind;
    cell.backgroundColor = [UIColor colorWithRed:1.000 green:0.571 blue:0.963 alpha:1.000];
    return cell;
    
}



//tableview的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return self.zhekoumerArr.count;
    }
       else if(section==1)
    {
        if (self.workitemArr.count == 0) {
            return 0;
        }else{
            return 1;
        }
    }else
    {
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==0) {
        static NSString *reuse =@"reuse";
        zhikouonePageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[[zhikouonePageTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse]autorelease];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        zhekoumerModel *zkmer = self.zhekoumerArr[indexPath.row];
        [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:zkmer.logo_path] placeholderImage:[UIImage imageNamed:@"占位图"]];
//        [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:zkmer.logo_path]
//         ];
        cell.titleLabel.text =zkmer.name;
        cell.addressLabel.text = zkmer.address;
        return cell;
        
    }
    else if(indexPath.section==1)
    {
        static NSString *reuse1 =@"reuse1";
        zhikoutwoPageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse1];
        if (!cell) {
            cell = [[[zhikoutwoPageTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse1]autorelease];
            //        placesModel *pl =self.placesArr[0];
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        NSArray *arr = @[cell.paisheLabel, cell.fuzhuangLabel, cell.changjingLabel, cell.chengpinLabel];
        NSArray *arr1 = @[cell.paishetextLabel, cell.fuzhuangtextLabel, cell.changjingtextLabel, cell.chengpintextLabel];
        NSInteger min = self.workitemArr.count < 4 ? self.workitemArr.count : 4;
        for (NSInteger i = 0; i < min; i++) {
            UILabel *label = arr[i];
            UILabel *label1 = arr1[i];
            zhekouworkitemModel *zkitem = self.workitemArr[i];
            label.text = zkitem.name;
            label1.text = zkitem.describe;
        }
        
        
//        zhekouworkitemModel *zkitem1 = self.workitemArr[0];
//        zhekouworkitemModel *zkitem2 =self.workitemArr[1];
//        zhekouworkitemModel *zkitem3 =self.workitemArr[2];
//        zhekouworkitemModel *zkitem4 =self.workitemArr[3];
        cell.fuwuLabel.text =@"套餐详情 :";
        // cell.fuwutextLabel.text =pl.name;
//
//        cell.paisheLabel.text =zkitem1.name;
//        cell.paishetextLabel.text =zkitem1.describe;

//        cell.fuzhuangLabel.text =zkitem2.name;
//        cell.fuzhuangtextLabel.text =zkitem2.describe;
//        
//        cell.changjingLabel.text =zkitem3.name;
//        cell.changjingtextLabel.text =zkitem3.describe;
//        
//        cell.chengpinLabel.text =zkitem4.name;
//        cell.chengpintextLabel.text =zkitem4.describe;
        return cell;
    }else{
        return nil;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 150;
    }
    else if(indexPath.section==1)
    {
        return 1200;
    }else{
        return 0;
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
